package com.tianya.common.taglib;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.*;
import java.util.List;
import com.tianya.common.util.CodeUtils;
import com.tianya.common.util.Utility;
import com.tianya.common.web.bean.*;

public class LoadCodeLabelTag extends javax.servlet.jsp.tagext.SimpleTagSupport {

	private String id;
	private String code;
    private String lang;
	public String getLang() {
		return lang;
	}

	public void setLang(String lang) {
		this.lang = lang;
	}
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	@SuppressWarnings("unchecked")
	public void doTag() throws JspException, IOException {
		List<Map> codeList ;
		
		
		codeList = CodeUtils.GetCodeList((PageContext) this.getJspContext(),this.code);	
		if(Utility.isEmpty(lang)){
			lang ="CN";
		}
		List<CodeOption> options = new ArrayList<CodeOption>();
		for (Map cd : codeList) {
			CodeOption co = new CodeOption(String.valueOf(cd.get("ID")), String.valueOf(cd.get("NAME")));
			if(this.lang.equalsIgnoreCase(String.valueOf(cd.get("lang")))){
				co.setValue2(String.valueOf(cd.get("NAME2")));
				co.setValue3(String.valueOf(cd.get("NAME3")));
				options.add(co);
			}
		}
		Iterator<CodeOption> iter = (Iterator<CodeOption>) options.iterator();
		this.getJspContext().setAttribute(id, iter, PageContext.REQUEST_SCOPE);

	}
}